package 位运算;

public class 丢失的数字7 {
    //https://leetcode.cn/problems/missing-number/description/
    public int missingNumber1(int[] nums) {
        int[] hash = new int[nums.length + 1];
        for (int i : nums)
            hash[i]++;
        for (int i = 0; i < hash.length; i++)
            if(hash[i] == 0) return i;
        return -1;
    }
    public int missingNumber2(int[] nums) {
        int sum1 = (0 + nums.length) * (nums.length + 1) / 2;
        int sum2 = 0;
        for (int i : nums)
            sum2 += i;
        return sum1 - sum2;
    }
    public int missingNumber(int[] nums) {
        int sum = 0;
        for (int i : nums)
            sum ^= i;
        for (int i = 0; i <= nums.length; i++)
            sum ^= i;
        return sum;
    }
}
